package org.wangwei.syntax;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * try catch finally return ?
 *
 * @author wangwei-ww
 * @Date 2018/1/22 9:36
 * @Comment
 */
public class TryCatchFinallyDemo {

    @Test
    public void testTryCatchSequenece() {
//        System.out.println(test1());
//        System.out.println(test11());
//        System.out.println(test2());
//        System.out.println(test3());
//        System.out.println(test4());
//        System.out.println(test5());
        System.out.println(test6());
    }


    private int test1() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
        }
        return b;
    }

    private String test11() {
        try {
            System.out.println("try block");
            return test12();
        } finally {
            System.out.println("finally block");
        }
    }

    private String test12() {
        System.out.println("return statement");
        return "after return";
    }

    private int test2() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }

            return 200;
        }
    }

    private int test3() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            b = 150;
        }

        return b;
    }

    private String test4() {
        return getMap().get("KEY").toString();
    }

    private Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        map.put("KEY", "INIT");
        try {
            map.put("KEY", "TRY");
            return map;
        } catch (Exception e) {
            map.put("KEY", "CATCH");
        } finally {
            map.put("KEY", "FINALLY");
            map = null;
        }

        return map;
    }

    private int test5() {
        int b = 20;
        try {
            System.out.println("try block");
            b = b / 0;
            return b += 80;
        } catch (Exception e) {
            b += 15;
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            b += 50;
        }

        return b;
    }

    private int test6() {
        int b = 20;
        try {
            System.out.println("try block");
            b = b / 0;
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
            return b += 15;
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            b += 50;
        }
    }

}
